package com.shwread.android.ui.widget;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000136.R;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.zlibrary.ui.android.view.LQReadingView;

public class PopWindowSelection
        implements
        OnClickListener
{
	private volatile PopupWindow      popupWindow;
	private TextView                  mCopyBtn;
	private TextView                  mHightLightBtn;
	private TextView                  mRemarkBtn;
	private TextView                  mShareBtn;
	private Context                   context;
	private volatile View             curMyView;
	private LinearLayout              mPopRl;
	private int                       screenWidth;
	private int                       screenHeight;
	private static PopWindowSelection instance;
	
	public static PopWindowSelection getInstance(Context context) {
		if (instance == null) instance = new PopWindowSelection(context);
		instance.context = context;
		return instance;
	}
	
	private PopWindowSelection(Context context) {
		curMyView = LayoutInflater.from(context).inflate(R.layout.reading__readingselect_alertdialog, null);
		init(context, curMyView);
	}
	
	private void init(Context context, View contentView) {
		this.context = context;
		DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.color.common_balck_transparent));
		popupWindow.setOutsideTouchable(true);// 设置pWindow之外的布局可点击
		popupWindow.setTouchInterceptor(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
				{
					popupWindow.dismiss();
					clearSelection();
					return true;
				}
				return false;
			}
		});
		initView(contentView);
		addListener();
		popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
	}
	
	private void clearSelection() {
		((ReadingBookActivity) context).myFBReaderApp.getTextView().clearSelection();
	}
	
	public boolean isShow() {
		return popupWindow.isShowing();
	}
	
	public void hidePop(boolean isShow) {
		if (isShow)
		{
			popupWindow.dismiss();
		}
	}
	
	protected void addListener() {
		mCopyBtn.setOnClickListener(this);
		mHightLightBtn.setOnClickListener(this);
		mRemarkBtn.setOnClickListener(this);
		mShareBtn.setOnClickListener(this);
	}
	
	protected void initView(View parentView) {
		mPopRl = (LinearLayout) parentView.findViewById(R.id.pop_rl);
		mCopyBtn = (TextView) parentView.findViewById(R.id.pop_copy);
		mHightLightBtn = (TextView) parentView.findViewById(R.id.pop_highlight);
		mRemarkBtn = (TextView) parentView.findViewById(R.id.pop_remark);
		mShareBtn = (TextView) parentView.findViewById(R.id.pop_share);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.pop_copy:
				//				Utils.showToast(context, "复制");
				((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SELECTION_COPY_TO_CLIPBOARD);
				hide();
				break;
			case R.id.pop_highlight:
				//				Utils.showToast(context, "高亮");
				((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SELECTION_HIGHLIGHT);
				hide();
				break;
			case R.id.pop_remark:
				//				Utils.showToast(context, "笔记");
				((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SELECTION_BOOKREMARK);
				hide();
				break;
			case R.id.pop_share:
				//				Utils.showToast(context, "分享");
				((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SELECTION_SHARE);
				hide();
				break;
		}
	}
	
	public void show2(View viewParent, int y, int startY) {
		int showX = screenWidth / 2;
		int showY = 0;
		//		popupWindow.setWidth(tvWidth < screenWidth ? tvWidth : WindowManager.LayoutParams.FILL_PARENT);
		if (y < screenHeight / 2)
		{
			//处于上半屏幕，显示在图标下面
			showY = y + LQReadingView.ICON_HEIGHT;
			popupWindow.showAtLocation(viewParent, Gravity.TOP, 0, showY);
		}
		else
		{
			//处于下半屏幕，显示在图标上面
			popupWindow.showAtLocation(viewParent, Gravity.BOTTOM, 0, screenHeight - startY);
		}
		
	}
	
	public void hide() {
		popupWindow.dismiss();
		((ReadingBookActivity) context).myFBReaderApp.setPopWindowSelection(null);
	}
}
